//
//  SKTabBar.m
//  WeiBo(MVVM-OC)
//
//  Created by yanli on 16/7/15.
//  Copyright © 2016年 mountainPassCell. All rights reserved.
//

#import "SKTabBar.h"

@interface SKTabBar ()
//  撰写按钮
@property (nonatomic, strong) UIButton *composeBtn;

@end


@implementation SKTabBar

#pragma mark -- 懒加载 撰写按钮
- (UIButton *)composeBtn {
    
    if (!_composeBtn) {
        //创建  UIButton
        _composeBtn = [UIButton new];
        
        //添加 UIButton 的点击事件
        [_composeBtn addTarget:self action:@selector(composeBtnAction) forControlEvents:UIControlEventTouchUpInside];
        
        //设置图片
        [_composeBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
        [_composeBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
        //设置 背景图片
        [_composeBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
        [_composeBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
        //设置 大小
        [_composeBtn sizeToFit];
        
    }
    
    return _composeBtn;
}


#pragma mark -- 点击事件
- (void)composeBtnAction {
    //两种方式
    //执行Block
    if (self.composeBtnClosure) {
         self.composeBtnClosure();
    }
    
    //代理调用
    [_skDelegate didSelectedComposeBtn];
}


// 重写 init 方法加载 撰写按钮 Btn
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        [self addSubview:self.composeBtn];
    }
    return self;
}


//  撰写按钮 布局
- (void)layoutSubviews {
    [super layoutSubviews];
    //设置撰写的中心点
    self.composeBtn.centerX = self.frame.size.width *0.5;
    self.composeBtn.centerY = self.frame.size.height *0.5;
    // 获取系统控件
    // 计算控件的宽度
    CGFloat itemWidth = self.frame.size.width / 5;
    // 记录遍历是第一个按钮
    int index = 0;
    for (UIView* value in self.subviews) {
        //  这个类是私有 UITabBarButton.self 获取class
        if ([value isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            value.x = (CGFloat)index * itemWidth;
            value.width = itemWidth;
            if (index == 1) {
                index++;
            }
            index++;
        }
    }
    
}




@end











